<?php
/**
 * class App_Controller_Index
 *
 * @author kim
 */
class App_Controller_Index extends App_Controller_Abstract
{
    /**
     * default action
     */
    public function indexAction()
    {
        $form = new App_Form_Blog_AddQuestion();
        if ($this->getRequest()->isPost()) {
            $data = $this->getRequest()->getPost();
            //var_dump($data);die;
            if ($form->isValid($data)) {
                $values = $form->getValues();
                $blog = new App_Model_Blog();
                $questionId = $blog->addQuestion($values);

                $this->_helper->redirector('index', 'index');
            } else {
                $form->populate($data);
            }
        }
        $this->view->formAddQuestion = $form;

        // try to get current user
        $userModel = new App_Model_User();
        if ($myUser = App_Model_User::getStoredMyUser()) {
            $this->view->user = $myUser;
        }
        $this->view->ip = $this->_request->getClientIp();

        // get blogs
        $blogModel = new App_Model_Blog();
        //var_dump($blogs);die;

        $page = $this->_getParam('page', 1);
        $count = $this->_getParam('count', 2);
        $result = $blogModel->getQuestions(array(), $page, $count);
        if ($result['total_count']) {
            $paginator = App_Model_Pagination::factory($result['results'], $result['total_count'], $page, $count);
            $this->view->paginator = $paginator;
        }
    }

    /**
     * delete the post
     */
    public function deletePostAction()
    {
        $this->_redirectIfNotLoggedIn();
        $blogId = $this->_getParam('id');
        if ($blogId) {
            $blogModel = new App_Model_Blog();
            $result = $blogModel->deleteQuestion($blogId);
        }
        $this->_helper->redirector('index', 'index');
    }

    /**
     * save the post
     */
    public function savePostAction()
    {
        //$this->_redirectIfNotLoggedIn();
    }
}
